package com.shunbo.convenient.controller;

import com.shunbo.convenient.resultVo.ProductIndexVo;
import com.shunbo.convenient.service.*;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.vo.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author weizhuang
 * api 接口
 */
@RestController
@RequestMapping("/manager")
public class ManagerController {

    @Resource
    private OrdersService ordersService;
    @Resource
    private CustomerInfoService customerInfoService;
    @Resource
    private ProductOrderService productOrderService;
    @Resource
    private ProductService productService;
    @Resource
    private BlessingService blessingService;

    /**
     * 首页数据接口
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return map
     */
    @RequestMapping("/orderIndex")
    public ConvenientResult orderIndex(String startTime,String endTime){

        Double money = 0.0;

        Map<String,Object> resultMap = new HashMap<>();
        List<Orders> between = ordersService.findBetween(startTime, endTime,null);
        resultMap.put("volume",between.size());
        for (Orders order:between
             ) {
            ProductOrder productOrder = new ProductOrder();
            productOrder.setOid(order.getId()+"");
            List<ProductOrder> all = productOrderService.findAll(productOrder);
            for (ProductOrder po: all
                 ) {
                if(po.getPid() != null){
                    Product byId = productService.findById(po.getPid() + "");
                    money+= po.getNum()*byId.getMoney();
                }
                if(po.getBid() != null){
                    Blessing byId = blessingService.findById(po.getBid() + "");
                    money+= po.getNum()*byId.getMoney();
                }
            }
        }
        resultMap.put("money",money);
        List<CustomerInfo> all = customerInfoService.findAll(null);
        resultMap.put("Average",money/all.size());

        return ConvenientResult.build(200, "返回结果", resultMap);
    }

    /**
     * 首页数据商品接口
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return map
     */
    @RequestMapping("/productIndex")
    public ConvenientResult productIndex(String startTime,String endTime){


        List<ProductIndexVo> list = new ArrayList<>();
        Map<String,Integer> resultMap = new HashMap<>();
        List<Orders> between = ordersService.findBetween(startTime, endTime,null);
        for (Orders order:between
        ) {
            ProductOrder productOrder = new ProductOrder();
            productOrder.setOid(order.getId()+"");
            List<ProductOrder> all = productOrderService.findAll(productOrder);
            for (ProductOrder po: all
            ) {
                if(resultMap.get(po.getPid()+"") !=null ){
                    System.out.println(po.getPid());
                    System.out.println(resultMap.get(po.getPid()+""));
                    resultMap.put(po.getPid()+"",resultMap.get(po.getPid()+"")+po.getNum());
                }else{
                    resultMap.put(po.getPid()+"",po.getNum());
                }
            }
        }
        if(resultMap.isEmpty()){
            return ConvenientResult.build(200, "返回结果", null);
        }
        Map<String,Integer> maps = sortMapByValue(resultMap);
        for (Map.Entry<String, Integer> entry : maps.entrySet()) {
            ProductIndexVo productIndexVo = new ProductIndexVo();
            System.out.println(entry.getKey()+"---"+entry.getValue());
            productIndexVo.setNum(entry.getValue());
            Product byId = productService.findById(entry.getKey() + "");
            productIndexVo.setProduct(byId);
            list.add(productIndexVo);
        }
        return ConvenientResult.build(200, "返回结果", list);
    }

    public static Map<String, Integer> sortMapByValue(Map<String, Integer> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, Integer> sortedMap = new LinkedHashMap<>();
        List<Map.Entry<String, Integer>> entryList = new ArrayList<>(
                oriMap.entrySet());
        Collections.sort(entryList, new MapValueComparator());

        Iterator<Map.Entry<String, Integer>> iter = entryList.iterator();
        Map.Entry<String, Integer> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }

}
